<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTaskApplicationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('task_applications', function (Blueprint $table) {
            $table->unsignedBigInteger('id')->primary()->comment('雪花ID');
            $table->string('application_number')->unique()->comment('报名编号');
            $table->unsignedBigInteger('task_id'); // 任务ID
            $table->unsignedBigInteger('tenant_id')->comment('租户ID');
            $table->unsignedBigInteger('freelancer_id')->nullable(); // 自由职业者ID（支持匿名报名）
            $table->string('name')->comment('姓名');
            $table->string('phone', 20)->comment('手机号');
            $table->string('email')->comment('邮箱');
            $table->string('id_card', 18)->comment('身份证号');
            $table->enum('gender', ['male', 'female'])->comment('性别');
            $table->date('birth_date')->comment('出生日期');
            $table->string('address', 500)->comment('地址');
            $table->text('experience')->nullable()->comment('工作经验');
            $table->json('skills')->nullable()->comment('技能');
            $table->enum('education', ['high_school', 'college', 'bachelor', 'master', 'doctor'])->nullable()->comment('学历');
            $table->string('emergency_contact')->comment('紧急联系人');
            $table->string('emergency_phone', 20)->comment('紧急联系人电话');
            $table->enum('status', ['pending', 'approved', 'rejected', 'completed', 'cancelled', 'dimission'])->default('pending'); // 申请状态
            $table->text('application_note')->nullable(); // 申请备注
            $table->text('rejection_reason')->nullable(); // 拒绝原因
            $table->text('review_note')->nullable()->comment('审核备注');
            $table->datetime('applied_at'); // 申请时间
            $table->datetime('processed_at')->nullable(); // 处理时间
            $table->timestamp('reviewed_at')->nullable()->comment('审核时间');
            $table->unsignedBigInteger('processed_by')->nullable(); // 处理人ID
            $table->string('source', 50)->default('web')->comment('报名来源');
            $table->timestamps();
            
            // 外键和索引
            $table->foreign('task_id')->references('id')->on('tasks')->onDelete('cascade');
            $table->foreign('tenant_id')->references('id')->on('tenants')->onDelete('cascade');
            $table->foreign('freelancer_id')->references('id')->on('freelancers')->onDelete('cascade');
            
            // 索引
            $table->index(['application_number']);
            $table->index(['phone']);
            $table->index(['id_card']);
            $table->index(['email']);
            $table->index(['status']);
            $table->index(['source']);
            $table->index(['tenant_id']);
            $table->index(['task_id', 'status']);
            $table->index(['task_id', 'tenant_id']);
            $table->index(['freelancer_id', 'status']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('task_applications');
    }
}
